import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';
import { CommonService } from '../common/common.service';
import { ASYNC_URLS } from '../r-mobile-url';

@Component({
  selector: 'oy-login',
  templateUrl: './login.component.html',
  styleUrls: ['./login.component.scss']
})
/**
 * 描述：登录界面TS
 * 公司：xxx有限公司
 * 版权：xxx2020
 * 作者：oyjl
 * 时间：2020-05-06 12:37:13
 */
export class LoginComponent implements OnInit {

  public accountName: string; // 用户名
  public password: string; // 密码
  public errorInfo: string; // 错误提示
  public showProtocol: boolean; // 展示协议弹框
  constructor(
    public commonService: CommonService,
    public router: Router) { }

  ngOnInit() {
    this.showProtocol = false;
    if (sessionStorage.getItem('accountId')) {
      this.router.navigateByUrl('/mobile/home');
    }
  }

  /**
   * 描述：显示隐藏密码控制
   * 作者：oyjl
   * 时间：2020-05-06 12:37:36
   */
  public pwdHandle(pwd, pwdIcon) {
    pwdIcon.classList.toggle('fa-eye-slash');
    pwdIcon.classList.toggle('fa-eye');
    if (pwdIcon.classList.contains('fa-eye')) {
      pwd.type = 'text';
    } else {
      pwd.type = 'password';
    }
  }

  /**
   * 描述：登录控制
   * 作者：oyjl
   * 时间：2020-05-06 13:48:13
   */
  public login() {
    const req = {
      loginMode: '1',
      systemType: 'rmoa',
      loginName: this.accountName,
      password: this.password
    };
    this.commonService.sendPost(ASYNC_URLS.login, req).subscribe((res: any) => {
      if (res.operateSuccess) {
        this.errorInfo = undefined;
        sessionStorage.setItem('accountId', res.accountId); // 设置accountId
        document.cookie = 'access_token_' + location.port + '=' + res.accessToken;
        document.cookie = 'refresh_token_' + location.port + '=' + res.refreshToken;
        this.router.navigateByUrl('/mobile/home');
      } else {
        this.errorInfo = res.opMsg || res.msg;
      }
    });
  }
}
